package com.itheima06;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;

/*
* 通过配置文件运行方法
* */
public class ReflectDemo2 {
    public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
//        Student s = new Student();
//        s.study();
//
//        Teacher t = new Teacher();
//        t.teach();

        /*
        * class.txt 文件中配置
        * className=xx
        * methodName=xx
        * */
        // 加载数据
        Properties properties = new Properties();
        FileReader fr = new FileReader("/Users/oscar/Desktop/HQWork/学习资料/code/MyReflect/class.txt");
        properties.load(fr);
        fr.close();

        // 根据键获取值
        String className = properties.getProperty("className");
        String methodName = properties.getProperty("methodName");

        // 用反射来使用
        Class<?> c = Class.forName(className);

        Constructor<?> constructor = c.getConstructor();
        Object o = constructor.newInstance();

        Method method = c.getMethod(methodName);
        method.invoke(o);

    }
}
